<?php

if (!class_exists("PST_DRAG_DROP")) {

    class PST_DRAG_DROP extends PST_PRESENTATION {

        public function __construct($settings) {
            parent::__construct($settings);
            $this->js_initial = "drag_drop_init";
        }

        public function renderMaintForm() {
            $html = '<div class="input textarea">' . "\n";
            $html.= '<label for="PermisoAction">' . $this->label . '</label>' . "\n";
            $html.= '<input name="data[Permiso][action]" type="hidden" id="PermisoAction">';
            $html.= '<div id="dnd_drop_zone">Suelta los archivos aquí</div>';
            $html.= '<output id="dnd_files_box"><table id="dnd_files_table" class="drag_drop"></table></output>';
            $html.= '</div>' . "\n";
            return $html;
        }

        public function jsIncludes() {
            return array("presentation/drag_drop.js");
        }

        function ajaxUploadFile() {
            if (empty($_REQUEST['name']))
                return json_encode(array("status" => "error", "desc" => 'Name required'));
            if (!preg_match('/^[-a-z0-9_][-a-z0-9_.]*$/i', $_REQUEST['name']))
                return json_encode(array("status" => "error", "desc" => 'Name error'));
            if (!isset($_REQUEST['index']))
                return json_encode(array("status" => "error", "desc" => 'Index required'));
            if (!preg_match('/^[0-9]+$/', $_REQUEST['index']))
                return json_encode(array("status" => "error", "desc" => 'Index error'));
            if (empty($_FILES['file']))
                return json_encode(array("status" => "error", "desc" => 'Upload required'));
            if ($_FILES['file']['error'] != 0)
                return json_encode(array("status" => "error", "desc" => 'Upload error'));

            $target = ROOT . '/' . APP_DIR . '/tmp/files/' . $_REQUEST['name'] . '-' . $_REQUEST['index'];
            move_uploaded_file($_FILES['file']['tmp_name'], $target);

            // Might execute too quickly
            usleep(200);

            return json_encode(array("status" => "ok"));
        }

        function ajaxMergeFile() {
            if (empty($_REQUEST['name']))
                return json_encode(array("status" => "error", "desc" => 'Name required'));
            if (!preg_match('/^[-a-z0-9_][-a-z0-9_.]*$/i', $_REQUEST['name']))
                return json_encode(array("status" => "error", "desc" => 'Name error'));
            if (!isset($_REQUEST['index']))
                return json_encode(array("status" => "error", "desc" => 'Index required'));
            if (!preg_match('/^[0-9]+$/', $_REQUEST['index']))
                return json_encode(array("status" => "error", "desc" => 'Index error'));

            $tmp_root = ROOT . '/' . APP_DIR . '/tmp/files/';
            $tmp_target = $tmp_root . $_REQUEST['name'];

            $extension = strtolower(substr($_REQUEST['name'], strrpos($_REQUEST['name'], ".") + 1));
            $arch = md5($_REQUEST['name'] . time()) . "." . $extension;

            $dst = fopen($tmp_root . $arch, 'wb');

            for ($i = 0; $i < $_REQUEST['index']; $i++) {
                $slice = $tmp_target . '-' . $i;
                $src = fopen($slice, 'rb');
                stream_copy_to_stream($src, $dst);
                fclose($src);
                unlink($slice);
            }

            fclose($dst);

            return json_encode(array("status" => "ok", "file" => $arch));
        }

        function ajaxDeleteFile() {
            if (empty($_REQUEST['filename']))
                return json_encode(array("status" => "error", "desc" => 'Filename error'));

            if (file_exists(ROOT . '/' . APP_DIR . '/tmp/files/' . $_REQUEST['filename'])) {
                if (unlink(ROOT . '/' . APP_DIR . '/tmp/files/' . $_REQUEST['filename'])) {
                    return json_encode(array("status" => "ok"));
                }
                return json_encode(array("status" => "error", "desc" => "Unlink error"));
            }

            return json_encode(array("status" => "error", "desc" => "File error"));
        }

        function ajaxDeleteMovedFile() {
            if (empty($_REQUEST['filename']))
                return json_encode(array("status" => "error", "desc" => 'Filename error'));
            
            global $MODEL;
            $MODEL->Query("DELETE FROM files WHERE filename='" . $_REQUEST['filename'] . "'");

            $subpath = "";
            for ($j = 0; $j < 5; $j++) {
                $subpath.= substr($_REQUEST['filename'], ($j * 2), 2) . "/";
            }
            
            if (file_exists(WWW_ROOT . 'files/' . $subpath . $_REQUEST['filename'])) {
                if (unlink(WWW_ROOT . 'files/' . $subpath . $_REQUEST['filename'])) {
                    return json_encode(array("status" => "ok"));
                }
                return json_encode(array("status" => "error", "desc" => "Unlink error"));
            }

            return json_encode(array("status" => "error", "desc" => "File error"));
        }

    }

}